package util;
import com.sun.cnpi.rss.elements.Rss;
import com.sun.cnpi.rss.parser.RssParser;
import com.sun.cnpi.rss.parser.RssParserException;
import com.sun.cnpi.rss.parser.RssParserFactory;

import java.io.IOException;
import java.net.MalformedURLException;
import java.io.File;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;

import com.sun.cnpi.rss.elements.Item;
import com.sun.cnpi.rss.elements.Rss;
import com.sun.cnpi.rss.parser.RssParser;
import com.sun.cnpi.rss.parser.RssParserFactory;
/*
 * RssRead.java
 *
 * Created on 16 de julio de 2007, 11:21
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */


/**
 *
 * @author mauri
 */
public class RssRead {

    
    /** Creates a new instance of RssRead */
    public RssRead() {
    }
    
    
    public static void main (String[] args)
    {
        RssRead m =new RssRead();
        try {

            m.getRSSImages("http://ar.rd.yahoo.com/fp/nws/rss/*http://d.yimg.com/ar.rss.news.yahoo.com/rss/tecnologia");
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }


    
    int refreshRSS;
    private String[] RSSFeeds = new String[1];
    private String tempDir = "";
    private long rssUpdate;
    private boolean match;
    

    public Vector getRSSImages(String RSSurl) throws Exception{
        Vector newImages = new Vector();

        
        
        RssParser parser = RssParserFactory.createDefault();
        Rss rss = parser.parse(new URL(RSSurl));
        Object[] items = new Item[1];
        items = rss.getChannel().getItems().toArray(items);
        String title;
        String link;
        String description;
        String owner = "";
        String guid;

        for(int i=0;i<items.length;i++){
            title = ((Item)items[i]).getTitle().toString().replaceAll("\\<.*?\\>","").trim();
            link = ((Item)items[i]).getLink().toString().replaceAll("\\<.*?\\>","").trim();
            description = ((Item)items[i]).getDescription().toString().replaceAll("\\<.*?\\>","").trim();
            owner = ((Item)items[i]).getDescription().toString().replaceAll("\\<.*?\\>","").trim();
            guid = "" + ((Item)items[i]).getGuid().toString().trim().hashCode();
            newImages.add(new ImageInfo(link, tempDir, title,description, guid, owner));
        }
        return newImages;

    }
    
 
/*
    public void initialize(String tempDir, String[] RSSFeeds, long rssUpdate, Vector currentImages){
        try{
            this.tempDir = tempDir;
            this.RSSFeeds = RSSFeeds;
            this.rssUpdate = rssUpdate;
            this.currentImages = currentImages;

            //get all the imagaes in the directory and add them to currentImages
            File imagesDirectory = new File(tempDir);
            if(!imagesDirectory.isDirectory()){
                imagesDirectory.mkdirs();
            }
            String[] tempArray = imagesDirectory.list();

            //go through the currentImages and see if it contains each item in tempArray, if not, add it
            for(int i=0;i<tempArray.length;i++){
                if(tempArray[i].endsWith(".jpg")){
                    File tempImage = new File(tempDir + File.separator + tempArray[i]);
                    if (tempImage.length()==0){
                        tempImage.delete();
                    }
                    System.out.println("INITIALIZE(): I am adding an image named " + tempArray[i] + " to currentImages!");
                    currentImages.add(new ImageInfo(tempArray[i],tempDir));
                }
            }

        } catch (Exception e){
            e.printStackTrace();
        }
    }
 */

    
    
    
}
